<!-- 登录 -->
<template>
  <div class="formBox">
    <!-- <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item label="账号" prop="loginName">
        <el-input
          type="text"
          v-model="ruleForm.loginName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          style="width: 100%"
          type="primary"
          @click="submitForm('ruleForm')"
        >
          登陆
        </el-button>
      </el-form-item>
    </el-form> -->
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { clientId, grantType, redirectUri } from '@/utils/baseConfig'

export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        loginName: '',
        password: '',
      },
      rules: {
        loginName: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      redirect: '/',
      loading: null
    };
  },
  created() {
    this.loading = this.$loading({
      lock: true,
      text: '登陆中...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    });
    if(this.$route.query.redirect) {
      console.log(this.$route)
      this.redirect = this.$route.query.redirect + this.$route.pathname;
    }
    if(this.$route.query.code) {
      this.automaticLogin(this.$route.query.code)
    }
  },
  methods: {
		...mapActions(['userLogin', 'userLoginCode']),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = this.ruleForm;
          await this.userLogin(params);
          window.location.href = this.redirect;
        } else {
          return false;
        }
      });
    },
    // 拿到code自动登录
    automaticLogin(code) {
      // console.log(code)
      // return;
      const params = {
        code,
        clientId,
        grantType,
        redirectUri,
      }
      this.userLoginCode(params);
      this.loading.close();
    }
  },
};
</script>
<style lang="scss">
.formBox {
  width: 500px;
  margin: 200px auto;
}
</style>
